#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

export NODE_PATH := /opt/node-red/lib/node_modules
export PATH := /opt/node-red/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

node_bin=/opt/node-red/bin/node
npm_bin=/opt/node-red/bin/npm

nodejs_version=v10.17.0
#nodejs_version=v12.13.1 (node-red-contrib-gpio broken)
#	wget --directory-prefix /opt/node-red/ https://nodejs.org/dist/latest-v12.x/node-$(nodejs_version)-linux-armv7l.tar.xz

node_red_version=1.0.3
node_red_dashboard_version=2.19.2
node_red_node_serialport_version=0.10.0

node_red_contrib_gpio_version=0.14.0

johnny_five_version=1.3.1
beaglebone_io_version=4.0.1

%:
	dh $@

override_dh_auto_configure:
	mkdir -p /opt/node-red/
	tar xf ./debian/node-$(nodejs_version)-linux-armv7l.tar.xz -C /opt/node-red/ --strip-components 1
	echo "cleanup"...
	rm -rf /opt/node-red/include/node/openssl/archs/BSD-x86_64*/
	rm -rf /opt/node-red/include/node/openssl/archs/VC-WIN*/
	rm -rf /opt/node-red/include/node/openssl/archs/aix*/
	rm -rf /opt/node-red/include/node/openssl/archs/darwin*/
	rm -rf /opt/node-red/include/node/openssl/archs/linux-aarch64/
	rm -rf /opt/node-red/include/node/openssl/archs/linux-elf/
	rm -rf /opt/node-red/include/node/openssl/archs/linux-ppc*/
	rm -rf /opt/node-red/include/node/openssl/archs/linux-x*/
	rm -rf /opt/node-red/include/node/openssl/archs/linux*-s390x/
	rm -rf /opt/node-red/include/node/openssl/archs/solaris*/
	echo "node --version"
	$(node_bin) --version
	echo "/opt/node-red/bin/node --version"
	$(node_bin) --version
	echo "/opt/node-red/bin/node /opt/node-red/bin/npm --version"
	$(node_bin) $(npm_bin) --version
	echo "python2 --version"
	/usr/bin/python2 --version
	env | grep PATH
	echo "node-red"
	echo "Fake Install: node-red@$(node_red_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g --unsafe-perm=true --loglevel=error --arch=arm --prefix /tmp node-red@$(node_red_version)
	cp -v debian/use-systemd.diff /tmp/lib/node_modules/node-red/
	cd /tmp/lib/node_modules/node-red/ ; patch -p1 < /tmp/lib/node_modules/node-red/use-systemd.diff ; rm /tmp/lib/node_modules/node-red/use-systemd.diff
	cd /tmp/lib/node_modules/node-red/ ; TERM=dumb $(node_bin) $(npm_bin) install systemd@0.3.1
	cd /tmp/lib/node_modules/node-red/ ; TERM=dumb $(node_bin) $(npm_bin) pack
	echo "Real Install: node-red@$(node_red_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g /tmp/lib/node_modules/node-red/*.tgz --unsafe-perm=true --loglevel=error --arch=arm --prefix /opt/node-red --no-save
	rm -rf /tmp/lib/node_modules/node-red/
	echo "node-red-dashboard"
	echo "Fake Install: node-red-dashboard@$(node_red_dashboard_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g --unsafe-perm=true --loglevel=error --arch=arm --prefix /tmp node-red-dashboard@$(node_red_dashboard_version)
	cd /tmp/lib/node_modules/node-red-dashboard/ ; TERM=dumb $(node_bin) $(npm_bin) pack
	echo "Real Install: node-red-dashboard@$(node_red_dashboard_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g /tmp/lib/node_modules/node-red-dashboard/*.tgz --unsafe-perm=true --loglevel=error --arch=arm --prefix /opt/node-red --no-save
	rm -rf /tmp/lib/node_modules/node-red-dashboard/
	echo "node-red-node-serialport"
	echo "Fake Install: node-red-node-serialport@$(node_red_node_serialport_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g --unsafe-perm=true --loglevel=error --arch=arm --prefix /tmp node-red-node-serialport@$(node_red_node_serialport_version)
	cd /tmp/lib/node_modules/node-red-node-serialport/ ; TERM=dumb $(node_bin) $(npm_bin) pack
	echo "Real Install: node-red-node-serialport@$(node_red_node_serialport_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g /tmp/lib/node_modules/node-red-node-serialport/*.tgz --unsafe-perm=true --loglevel=error --arch=arm --prefix /opt/node-red --no-save
	rm -rf /tmp/lib/node_modules/node-red-node-serialport/
	echo "node-red-contrib-gpio"
	echo "Fake Install: node-red-contrib-gpio@$(node_red_contrib_gpio_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g --unsafe-perm=true --loglevel=error --arch=arm --prefix /tmp node-red-contrib-gpio@$(node_red_contrib_gpio_version)
	cd /tmp/lib/node_modules/node-red-contrib-gpio/ ; TERM=dumb $(node_bin) $(npm_bin) pack
	echo "Real Install: node-red-contrib-gpio@$(node_red_contrib_gpio_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g /tmp/lib/node_modules/node-red-contrib-gpio/*.tgz --unsafe-perm=true --loglevel=error --arch=arm --prefix /opt/node-red --no-save
	rm -rf /tmp/lib/node_modules/node-red-contrib-gpio/
	echo "johnny-five"
	echo "Fake Install: johnny-five@$(johnny_five_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g --unsafe-perm=true --loglevel=error --arch=arm --prefix /tmp johnny-five@$(johnny_five_version)
	cd /tmp/lib/node_modules/johnny-five/ ; TERM=dumb $(node_bin) $(npm_bin) pack
	echo "Real Install: johnny-five@$(johnny_five_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g /tmp/lib/node_modules/johnny-five/*.tgz --unsafe-perm=true --loglevel=error --arch=arm --prefix /opt/node-red --no-save
	rm -rf /tmp/lib/node_modules/johnny-five/
	echo "beaglebone-io"
	echo "Fake Install: beaglebone-io@$(beaglebone_io_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g --unsafe-perm=true --loglevel=error --arch=arm --prefix /tmp beaglebone-io@$(beaglebone_io_version)
	cd /tmp/lib/node_modules/beaglebone-io/ ; TERM=dumb $(node_bin) $(npm_bin) pack
	echo "Real Install: beaglebone-io@$(beaglebone_io_version)"
	TERM=dumb $(node_bin) $(npm_bin) install -g /tmp/lib/node_modules/beaglebone-io/*.tgz --unsafe-perm=true --loglevel=error --arch=arm --prefix /opt/node-red --no-save
	rm -rf /tmp/lib/node_modules/beaglebone-io/
	echo "move /opt/node-red/ to ./tmp/opt/node-red/"
	mkdir -p ./tmp/opt/node-red/
	mv /opt/node-red/* ./tmp/opt/node-red/

